package com.ccsoft.leetcode.controller;

import com.ccsoft.leetcode.model.TreeNode;
import com.ccsoft.leetcode.service.*;
import com.ccsoft.leetcode.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private RomanNumerals romanNumerals;
    @Autowired
    private CommonPrefix commonPrefix;
    @Autowired
    private Brackets brackets;
    @Autowired
    private StrStr strStr;
    @Autowired
    private LengthOfLastWord lengthOfLastWord;
    @Autowired
    private Increase1 increase1;
    @Autowired
    private AddBinary addBinary;
    @Autowired
    private Sqrt sqrt;
    @Autowired
    private ReversePolishNotation reversePolishNotation;
    @Autowired
    private Calculator calculator;
    @Autowired
    private SameTree sameTree;

    @GetMapping("romanNumerals")
    public JsonResult romanNumerals(){
        return JsonResult.ok(romanNumerals.romanToInt("IV"));
    }

    @PostMapping("commonPrefix")
    public JsonResult commonPrefix(@RequestBody String[] strArray){
//        String[] strArray = new String[]{"ab","a"};
        return JsonResult.ok(commonPrefix.longestCommonPrefix(strArray));
    }

    @PostMapping("brackets")
    public JsonResult brackets(@RequestBody String s){
        return JsonResult.ok(brackets.isValid(s));
    }

    //StrStr LengthOfLastWord
    @PostMapping("StrStr")
    public JsonResult StrStr(@RequestBody String[] strArray){
        return JsonResult.ok(strStr.strStr(strArray[0],strArray[1]));
    }

    @PostMapping("LengthOfLastWord")
    public JsonResult LengthOfLastWord(@RequestBody String s){
        return JsonResult.ok(lengthOfLastWord.lengthOfLastWord(s));
    }

    @PostMapping("Increase1")
    public JsonResult Increase1(@RequestBody int[] intArray){
        return JsonResult.ok(increase1.plusOne(intArray));
    }

    //AddBinary
    @PostMapping("addBinary")
    public JsonResult addBinary(@RequestBody Map<String,String> params){
        return JsonResult.ok(addBinary.addBinary(params.get("a"),params.get("b")));
    }

    @PostMapping("/sqrt")
    public JsonResult sqrt(@RequestBody int x){
        return JsonResult.ok(sqrt.mySqrt(x));
    }

    // Calculator
    @PostMapping("/reversePolishNotation")
    public JsonResult reversePolishNotation(@RequestBody String notation){
        return JsonResult.ok(reversePolishNotation.getReversePolishNotation(notation));
    }

    @PostMapping("/calculator")
    public JsonResult calculator(@RequestBody String notation){
        return JsonResult.ok(calculator.cal(notation));
    }

    @PostMapping("/polishStack")
    public JsonResult getPolishStack(@RequestBody String notation){
        return JsonResult.ok(calculator.cal(notation));
    }

    @PostMapping("/sameTree")
    public JsonResult sameTree(@RequestBody Map<String,TreeNode> params) {
        return JsonResult.ok(sameTree.isSameTree(params.get("p"),params.get("q")));
    }
}
